namespace TRXParserConsole
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    /// <summary>
    /// Doing analysis of all .trx files
    /// </summary>
    public class TRXParser
    {
        /// <summary>
        /// File's directory
        /// </summary>
        private string trxDir;

        /// <summary>
        /// Test data
        /// </summary>
        public int total, passed, error, failed;

        /// <summary>
        /// Tests with error
        /// </summary>
        public List<string> errorTests = new List<string>();

        /// <summary>
        /// Class constructor. Set durectory of trx files
        /// </summary>
        /// <param name="dir">Directory of files</param>
        public TRXParser(string dir)
        {
            this.trxDir = dir;
            this.total = this.passed = this.failed = this.error = 0;
        }

        /// <summary>
        /// Parsing all of files
        /// </summary>
        public int Parse()
        {
            string[] files = null;
            try
            {
                files = Directory.GetFiles(this.trxDir, "*.trx", SearchOption.AllDirectories);
            }
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
            if (files.Length == 0)
            {
                Console.WriteLine("В директории не обнаружено тестовых файлов");
                return -1;
            }
            foreach (string f in files)
            {
                TRXFileInfo trxFile = new TRXFileInfo(f);

                trxFile.GetInfo();

                this.total += trxFile.GetTotalTests();
                this.passed += trxFile.GetPassedTests();
                this.error += trxFile.GetErrorTests();
                this.failed += trxFile.GetFailedTests();
                if (trxFile.getErrorTestNames().Length > 0)
                {
                    foreach (string s in trxFile.getErrorTestNames())
                    {
                        this.errorTests.Add(s);
                    }
                }
            }
            return 1;
        }

        /// <summary>
        /// Print results on screen
        /// </summary>
        public void Printresults()
        {
            Console.WriteLine(" Проверено тестов : " + this.total);
            Console.Write("\n  -- удачно завершенных тестов   " + this.passed + " |");
            Console.Write("\n  -- тестов с ошибкой            " + this.error + " |");
            Console.Write("\n  -- неудачно завершенных тестов " + this.failed + " |");
            if (this.errorTests.Count > 0)
            {
                Console.Write("\n  Тесты, завершенные с ошибкой : \n");
                foreach (string s in this.errorTests)
                {
                    Console.WriteLine("    " + s);
                }
            }
            else
            {
                Console.WriteLine("\n  Тесты с ошибкой отсутствуют");
            }
        }
    }
}